angular.module('starter.controllers', [])
  .controller('tabCtrl', function (server, $scope) {
    $scope.jumpTo = function (des) {
      server.jumpTo(des);
    }
  })
  .controller('productCtrl', function ($scope, $stateParams, server) {
    $scope.order = {};
    server.productlist().then(function (data) {
      $scope.productlist = data.productlist;
      for (var i = 0; i < $scope.productlist.length; i++)
        if ($stateParams.productId == $scope.productlist[i].product_id)
          break;
      ;
      $scope.product = $scope.productlist[i];
    })
    $scope.add = function () {
      server.needLogin();
      var order = {};
      ['product_id', 'seller_name', 'product_name', 'product_price'].forEach(function (e) {
        order[e] = $scope.product[e];
      });
      order.product_number = 1;
      server.add_to_shoppingcart(order);
    }
  })

  .controller('product_listCtrl', function ($scope, server) {
    server.productlist().then(function (data) {
      $scope.productlist = data.productlist;
    })
  })
  .controller('shoppingcartCtrl', function ($scope, server) {
    $scope.shoppingcart = server.shoppingcart();
    $scope.order_add = function (index) {
      server.needLogin();
      var order = $scope.shoppingcart.shoppingcart[index];
      $scope.shoppingcart.shoppingcart.splice(index, 1);
      $scope.shoppingcart.shoppingcart.splice(index, 1);
      localStorage.shoppingcart = JSON.stringify($scope.shoppingcart);
      order.state = "未确认";
      var date = new Date();
      order.buy_time = date.toUTCString();
      server.order_add(order);
    }
    $scope.del = function (index) {
      $scope.shoppingcart.shoppingcart.splice(index, 1);
      localStorage.shoppingcart = JSON.stringify($scope.shoppingcart);
    }
  })
  .controller('order_managementCtrl', function ($scope, server) {
    $scope.jumpTo = function (des) {
      server.jumpTo(des);
    }
  })
  .controller('person_centerCtrl', function ($scope, server) {
    $scope.jumpTo = function (des) {
      server.jumpTo(des);
    }
    $scope.log_out = function () {
      server.log_out();
    }
    $scope.isLogin = (server.isLogin() == "true");
    console.log($scope.isLogin);
  })
  .controller('seller_managementCtrl', function ($scope, server) {
    server.needLogin();
  })
  .controller('seller_productCtrl', function ($scope, server) {
    server.needLogin();
    server.seller_productlist().then(function (data) {
      $scope.productlist = data.productlist;
    })
    $scope.modify = function(index){
      server.product_modify($scope.productlist[index]);
    }
  })
  .controller('seller_orderCtrl', function ($scope, server) {
    server.needLogin();
    $scope.orderlist = {};
    server.seller_order().then(function (data) {
      $scope.orderlist = data.OrderInfo;
    })
    $scope.modify = function (order_id, state) {
      server.order_modify(order_id, state);
    }

  })
  .controller('seller_product_addCtrl', function ($scope, server) {
    $scope.product = {}
    server.needLogin();
    $scope.add_product = function () {
      server.product_add($scope.product);
    }
  })
  .controller('order_allCtrl', function ($scope, server) {
    server.needLogin();
    $scope.orderlist = {};
    server.orderlist().then(function (data) {
      $scope.orderlist = data.OrderInfo;
    })
  })
  .controller('order_completedCtrl', function ($scope, server) {
    server.needLogin();
    $scope.orderlist = {};
    server.orderlist().then(function (data) {
      $scope.orderlist = data.OrderInfo;
    })
  })
  .controller('order_in_progressCtrl', function ($scope, server, $ionicPopover) {
    server.needLogin();
    server.orderlist().then(function (data) {
      $scope.orderlist = data.OrderInfo;
    })
    $scope.modify = function (order_id, state) {
      server.order_modify(order_id, state);
    }
    $scope.popovers = {};
    $scope.pay = function (order_id, total,type,$event) {
      if ($scope.popovers[order_id]) {
        $scope.popovers[order_id].show($event);
      } else {
        $ionicPopover.fromTemplateUrl('my-popover.html', {
          scope: $scope
        }).then(function (popover) {
          $scope.popovers[order_id] = popover;
          popover.show($event);
          popover.scope.order_id = order_id;
         if(type == 0) popover.scope.type = "支付";
         else  popover.scope.type = "确认";
          popover.scope.total = total;
          popover.scope.p_user = { type:type, order_id: order_id };
          popover.scope.pay = function () {
            console.log(popover.scope.p_user);
            console.log(popover.scope.order_id);
            server.pay(popover.scope.p_user).then(function (data) {
              if (data.code == 1 && type == 0) server.order_modify(order_id, "已付款");
              if (data.code == 1 && type == 1) server.order_modify(order_id, "已完成");;
            });
            $scope.popovers[order_id].remove();
            server.jumpTo('tab.order_management');
          }
        });
      }
    }
  })
  .controller('sign_inCtrl', function ($scope, server) {
    $scope.user = {}
    $scope.sign_in = function () {
      server.sign_in($scope.user);
    }
  })
  .controller('sign_outCtrl', function ($scope, server) {
    $scope.user = {};
    $scope.sign_out = function () { server.sign_out($scope.user) };
  })
  .controller('person_informationCtrl', function ($scope, server) {
    server.needLogin();
    $scope.modify = {}
    server.user_info().then(function (data) {
      $scope.user = data.userInfo;
    });
    $scope.modify_information = function () {
      if ($scope.modify.oldpwd == null) $scope.modify.oldpwd = "";
      if ($scope.modify.newpwd == null) $scope.modify.newpwd = "";
      if ($scope.modify.phonenumber == null) $scope.modify.phonenumber = $scope.user.phonenumber;
      if ($scope.modify.payname == null) $scope.modify.payname = $scope.user.payname;
      if ($scope.modify.address == null) $scope.modify.address = $scope.user.address;

      server.modify_information($scope.modify);
    }
  });
